//
//  VCRegisterNextViewController.m
//  VCShop
//
//  Created by VoyageCN on 16/8/18.
//  Copyright © 2016年 VoyageCN. All rights reserved.
//

#import "VCRegisterNextViewController.h"
#import "VCNextRegisterView.h"
#import "VCHttpTool.h"

@interface VCRegisterNextViewController ()

@property (nonatomic, strong) VCNextRegisterView *nextRegisterView;

@property (nonatomic, copy) NSString *code;

@end

@implementation VCRegisterNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = MainColor;
    self.title = @"验证手机号";
    self.edgesForExtendedLayout = 0;
    
    [self addControllers];
    [self requestCodeNumber];
}

- (void)addControllers {
    
    [self.view addSubview:self.nextRegisterView];
    
    __weak typeof(self) weakSelf = self;
    
    
    [_nextRegisterView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.view.mas_top);
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@140);
    }];
}

- (void)requestCodeNumber {
    
    [self POSTRequestWithURL:@"appMember/createCode.do"
                       param:@{@"MemberId":_userMessageDic[@"userName"]}
                   isSuccess:^(id responseObject) {
                       
                       if ([responseObject[@"result"] isEqualToString:@"success"]) {
                           [self.nextRegisterView GCDTime];
                       }else if ([responseObject[@"result"] isEqualToString:@"TelephoneExistError"]){
                           [self showTostInView:@"手机号已注册"];
                       }else{
                           [self showTostInView:@"验证码请求失败"];
                       }
                   } isError:^(NSError *error) {
                       VCLog(@"%@",error);
   }];
}

- (void)registerMethod:(NSString *)codeString {
    
    [self GETRequestWithURL:@"appMember/appRegistration.do" param:@{@"LoginName":_userMessageDic[@"userName"],
                                                                @"Lpassword":_userMessageDic[@"password"],
                                                                @"Code":codeString,
                                                                @"Telephone":_userMessageDic[@"userName"]}
              isSuccess:^(id responseObject) {
                  VCLog(@"注册：%@",responseObject);
                  if ([responseObject[@"result"] isEqual:@"success"]) {
                      [self showTostInView:@"注册成功"];
                      [self performSelector:@selector(returnMyview) withObject:nil afterDelay:1.0];
                  }else if([responseObject[@"result"] isEqual:@"codeError"]){
                      [self showTostInView:@"验证码错误"];
                  }else{
                      [self showTostInView:@"注册失败"];
                  }
              }
                isError:^(NSError *error) {
                    [self showTostInView:@"请检查网络"];
                }];
}


#pragma mark - 懒加载

- (VCNextRegisterView *)nextRegisterView {
    
    if (!_nextRegisterView) {
        _nextRegisterView = [[VCNextRegisterView alloc] init];
        _nextRegisterView.phoneNumText = _userMessageDic[@"userName"];
        __weak typeof(self) weakSelf = self;
        
        _nextRegisterView.showTimeBlock = ^(){
            [weakSelf requestCodeNumber];
        };
        _nextRegisterView.registerBlock = ^(NSString *code){
            
            [weakSelf registerMethod:code];
        };
    }
    return _nextRegisterView;
}

- (void)returnMyview{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
